#!/bin/bash

usage() {
    cat <<EOF
Usage: $0 -ch -d [ DIRECTORY ] -- [ TERM ... ]

Example:

    $0 cats
    $0 -h
    $0 -c -dl ~/pastes/ -- cats dogs
EOF
exit 0
}

err() {
    printf 'E: %s\n' "$@"
    exit 1
}

(( $# )) || usage

while (( $# )); do
    case "$1" in
        (-d) [[ $2 ]] || err "-d requires an argument"
            dir="$2";
            shift
            ;;
        (-c) dl=1;;
        (-h) usage;;
        (--) shift; break;;
        (*) terms+=("$1");;
    esac
    shift
done
terms+=("$@")

(( ${#terms[@]} )) || err "You forgot to give search terms."

SAVE_DIR=${dir:-~/.paste_bin_scraper}
mkdir -p -- "$SAVE_DIR"
echo "Files will be saved to $SAVE_DIR"

for term in "${terms[@]}"; do
    if (( dl )); then
        echo "Downloading files please wait"
        curl -s -- "https://psbdmp.ws/api/search/$term" \
            | jq -r '.data[].id' \
            | xargs -I{} -- wget -P "$SAVE_DIR" -nc -- https://pastebin.com/raw/{}
    else
        echo "Search term $term"
        echo "Total files: $(curl -s "https://psbdmp.ws/api/search/$term" | jq ".data" | wc -l)"
    fi

done
